clear 
set more off 
set matsize 8000 
cap log close 

cd "D:\Data\workdata\707116\SIDS_RDD\"

use "SIDS_analysis_final_full.dta", clear

log using "Revision1/log_files/08 Tables IV days.log", replace

drop di*
g dif = birthday - mdy(12,31,1991)

gen     di1 =   dif 
gen     di1_i = dif 
replace di1_i = 0 if dif<0

g dc_sids_oth = dc_sids + dc_all_other 

g 		mor1_exc_sids = mor1 - dc_sids_oth
replace mor1_exc_sids = 0 if mor1==0 

g 		mor5_exc_sids = mor5 - dc_sids_oth
replace mor1_exc_sids =0 if mor5==0 

g m_high_ed = m_short + m_mid + m_long
egen dc_other_non_sids = rowmax(dc_inf  dc_neo dc_nerv dc_resp)

g preterm = . 
replace preterm = 1 if fullterm37==0
replace preterm = 0 if fullterm37==1


label var mage "Mother age at birth" 
label var m_basic "Mother basic education" 
label var m_voc "Mother vocational education" 
label var m_high "Mother higher education" 
label var m_immigrant "Mother immigrant"  

label var female "Female" 
label var border "Birth order" 
label var preterm "Born preterm"
label var LBW "Low birthweight" 

label var dc_sids "SIDS mortality (over 10K)" 
label var dc_sids_oth "SIDS + all unclassified (over 10K)"
label var mor1 "Infant mortality" 
label var mor5 "Child mortality" 
label var mor1_exc_sids "Infant mortality except SIDS" 
label var mor5_exc_sids "Child mortality except SIDS" 


global outset1 "dc_sids dc_sids_oth mor1 mor5 mor1_exc_sids mor5_exc_sids"

drop if gage_wks==. 

g ebday  = birthday-(gage_wks*7)+280
format ebday %td

gen edif = ebday -  mdy(12,31,1991)

gen z = 0 
replace z=1 if edif>0

table ebday if inrange(edif,-10,10), stat(mean edif) stat(mean z)
table birthday if inrange(dif,-10,10), stat(mean dif) stat(mean treat)

 
gen edi1 = edif 
gen edi1_i=edif 
replace edi1_i=0 if edif<0


*bw come from Table 2 to be comparable - cct automated*

eststo clear 
local x = "dc_sids"
local bw = 1554
ivregress 2sls `x'  edi1* (treat=z) if inrange(edif,-`bw',`bw'), cluster(birthday) first
g N__`x'__`bw' = e(N)
test treat 
g b__`x'__`bw' = _b[treat]
g se__`x'__`bw' = _se[treat]
g p__`x'__`bw' = r(p)
qui sum `x' if e(sample) & treat==0
g M__`x'__`bw' = r(mean)

eststo clear 
local x = "dc_sids_oth"
local bw = 1334
ivregress 2sls `x'  edi1* (treat=z) if inrange(edif,-`bw',`bw'), cluster(birthday) first
g N__`x'__`bw' = e(N)
test treat 
g b__`x'__`bw' = _b[treat]
g se__`x'__`bw' = _se[treat]
g p__`x'__`bw' = r(p)
qui sum `x' if e(sample) & treat==0
g M__`x'__`bw' = r(mean)

eststo clear 
local x = "mor1"
local bw = 1799
ivregress 2sls `x'  edi1* (treat=z) if inrange(edif,-`bw',`bw'), cluster(birthday) first
g N__`x'__`bw' = e(N)
test treat 
g b__`x'__`bw' = _b[treat]
g se__`x'__`bw' = _se[treat]
g p__`x'__`bw' = r(p)
qui sum `x' if e(sample) & treat==0
g M__`x'__`bw' = r(mean)

eststo clear 
local x = "mor5"
local bw = 1818
ivregress 2sls `x'  edi1* (treat=z) if inrange(edif,-`bw',`bw'), cluster(birthday) first
g N__`x'__`bw' = e(N)
test treat 
g b__`x'__`bw' = _b[treat]
g se__`x'__`bw' = _se[treat]
g p__`x'__`bw' = r(p)
qui sum `x' if e(sample) & treat==0
g M__`x'__`bw' = r(mean)

eststo clear 
local x = "mor1_exc_sids"
local bw = 1533
ivregress 2sls `x'  edi1* (treat=z) if inrange(edif,-`bw',`bw'), cluster(birthday) first 
g N__`x'__`bw' = e(N)
test treat 
g b__`x'__`bw' = _b[treat]
g se__`x'__`bw' = _se[treat]
g p__`x'__`bw' = r(p)
qui sum `x' if e(sample) & treat==0
g M__`x'__`bw' = r(mean)

eststo clear 
local x = "mor5_exc_sids"
local bw = 1423
ivregress 2sls `x'  edi1* (treat=z) if inrange(edif,-`bw',`bw'), cluster(birthday) first 
g N__`x'__`bw' = e(N)
test treat 
g b__`x'__`bw' = _b[treat]
g se__`x'__`bw' = _se[treat]
g p__`x'__`bw' = r(p)
qui sum `x' if e(sample) & treat==0
g M__`x'__`bw' = r(mean)


keep b__* se__* p__* N__* M__* 
keep if _n==1 
xpose, clear varname 
rename v1 v_scalar 
split _varname, p("__")
drop _varname 
rename _varname1 coef_name 
rename _varname2 vname 
rename _varname3 bw 
destring bw, replace 

g    		group = . 

foreach x in $cset1 $cset2{
replace group = 1 if vname =="`x'" 	
}

foreach x in $outset1{
replace group = 2 if vname =="`x'"
}

order coef_name group vname bw v_scalar 

export excel using "Revision1\output\days\DTablesIV_rev1.xlsx", firstrow(variables) replace 
 
log close 